package com.hmall.util;

import com.hmall.model.SysUser;

import java.text.SimpleDateFormat;
import java.util.Date;


public class DataFomateUtil {




    public static SysUser stringToSysUser(String str) throws Exception{

        String[] arr = str.split(",");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



        SysUser user = new SysUser();
        user.setUserId(Integer.valueOf(arr[0]));
        user.setUsername(arr[1]);
        user.setPassword(arr[2]);
        user.setEmail(arr[3]);
        user.setPhone(arr[4]);
        user.setSalt(arr[5]);
        user.setIntroduction(arr[6]);
        user.setAvatar(arr[7]);



        if(arr[8].equals("null")){
            user.setDeptId(0);
        }
        else {
            user.setDeptId(Integer.valueOf(arr[8]));
        }

        user.setQuestion(arr[9]);
        user.setAnswer(arr[10]);


        Date date = null;

        if(arr[11].equals("null")){
            user.setCreateTime(date);
        } else {
            user.setCreateTime(sdf.parse(arr[11]));
        }

        if(arr[12].equals("null")){
            user.setUpdateTime(null);
        } else {
            user.setUpdateTime(sdf.parse(arr[12]));
        }
        user.setDelFlag(arr[13]);

        return user;
    }

    public static String sysUserToString(SysUser user){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String str = "";
        str = String.valueOf(user.getUserId()) + ',' + user.getUsername() + ',' + user.getPassword() + ',' + user.getEmail() + ',' + user.getPhone() + ',' + user.getSalt() + ',' + user.getIntroduction() + ',' + user.getAvatar() + ',';
        if(user.getDeptId()  == null){
            str += "null";
        } else{
            str += String.valueOf(user.getDeptId());
        }
        str += ',';
        str += user.getQuestion() + ',' + user.getAnswer() + ',';

        Date date = null;
        if(user.getCreateTime() == date){
            str += "null";
        } else{
            str += sdf.format(user.getCreateTime());
        }

        str += ',';

        if(user.getUpdateTime() == date){
            str += "null";
        } else{
            str += sdf.format(user.getUpdateTime());
        }

        str += ',' + user.getDelFlag();
        return str;
    }
}
